﻿using System.Linq;
using MediatR;
using ProduceBasicData.Api.Application.Command.Incomings;
using ProduceBasicData.Api.DTO;
using ProduceBasicData.Domain.Material;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.Incomings
{
    /// <summary>
    /// 来料表信息展示处理
    /// </summary>
    public class GetIncomingHandler : IRequestHandler<GetIncomingCommand, ApiPaging<IncomingDto>>
    {
        private readonly IBaseRespository<IncomingMaterials> incomRepo;
        private readonly IBaseRespository<SupplierModel> suppRepo;
        private readonly IBaseRespository<MaterialGroupModel> groupRepo;

        public GetIncomingHandler(IBaseRespository<IncomingMaterials> incomRepo,
            IBaseRespository<SupplierModel> suppRepo,
            IBaseRespository<MaterialGroupModel> groupRepo)
        {
            this.incomRepo = incomRepo;
            this.suppRepo = suppRepo;
            this.groupRepo = groupRepo;
        }

        public async Task<ApiPaging<IncomingDto>> Handle(GetIncomingCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<IncomingDto> res=new ApiPaging<IncomingDto>();

            var incom = incomRepo.GetAll().Where(x=>!x.Isdel);
            var supp = suppRepo.GetAll().Where(x => !x.Isdel);
            var groups = groupRepo.GetAll().Where(x => !x.Isdel);

            //编号
            if (!string.IsNullOrEmpty(request.IncomingCode))
            {
                incom = incom.Where(x=>x.IncomingCode.Contains(request.IncomingCode));
            }

            //供应商
            if (!string.IsNullOrEmpty(request.SupplierName))
            {
                supp = supp.Where(x => x.Name.Contains(request.SupplierName));
            }

            //群组
            if (!string.IsNullOrEmpty(request.MaterialName))
            {
                groups = groups.Where(x => x.GroupName.Contains(request.MaterialName));
            }

            //批次
            if (!string.IsNullOrEmpty(request.Batch))
            {
                incom = incom.Where(x => x.Batch.Contains(request.Batch));
            }

            //生效
            if (!string.IsNullOrEmpty(request.EffectiveDate))
            {
                incom = incom.Where(x=>Convert.ToString(x.EffectiveDate).Contains(request.EffectiveDate));
            }

            //失效
            if (!string.IsNullOrEmpty(request.ExpirationDate))
            {
                incom = incom.Where(x => Convert.ToString(x.ExpirationDate).Contains(request.ExpirationDate));
            }

            //创建人
            if (!string.IsNullOrEmpty(request.CreateName))
            {
                incom = incom.Where(x => x.CreateName.Contains(request.CreateName));
            }
            //创建日期
            if (!string.IsNullOrEmpty(request.CreateTime))
            {
                incom = incom.Where(x => Convert.ToString(x.CreateTime).Contains(request.CreateTime));
            }
            //更新日期
            if (!string.IsNullOrEmpty(request.UpdateTime))
            {
                incom = incom.Where(x => Convert.ToString(x.UpdateTime).Contains(request.UpdateTime));
            }

            var linq = from a in incom
                       join b in supp on a.SupplierId equals b.Id
                       join c in groups on a.GroupId equals c.Id
                       select new IncomingDto
                       {
                           Id = a.Id,
                           Batch=a.Batch,
                           CreateName=a.CreateName,
                           CreateTime=a.CreateTime,
                           EffectiveDate=a.EffectiveDate,
                           ExpirationDate=a.ExpirationDate,
                           GroupId=a.GroupId,
                           IncomingCode=a.IncomingCode,
                           MaterialName=c.MaterialName,
                           NUmbner=a.NUmbner,
                           SupplierId=a.SupplierId,
                           SupplierName=b.Name,
                           UpdateName=b.UpdateName,
                           UpdateTime=b.UpdateTime,
                       };
            res.PageDatas = linq.OrderByDescending(x=>x.CreateTime).Skip((request.Index-1)*request.Size).Take(request.Size).ToList();
            res.TotalCount = linq.Count();
            res.Code = ApiEnum.Success;
            res.Msg = "查询成功";
            return res;
        }
    }
}
